﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Extensions;
using TaoPortal.DataAccess;
using TaoPortal.Entity;

namespace TaoPortal.BusinessLogic.Repository
{
    public class ArticleRepository : BaseRepository
    {
        public ArticleRepository(DataContext dbcontext)
        {
            db = dbcontext;
            AutoCommit = true;
        }
        public ArticleRepository()
            : this(new DataContext())
        {

        }

        public void AddArticle(TP_Article article, TP_ArticleClass aClass, List<TP_ArticleBody> bodyList, List<TP_Tag> tagList, List<TP_ArticleImg> imgs)
        {
            db.AddEntity(article);
            db.AddEntity(aClass);
            foreach (var item in bodyList)
            {
                db.AddEntity(item);
            }
            foreach (var item in tagList)
            {
                var _tag = db.TP_Tag.Where(x => x.TagName == item.TagName).FirstOrDefault();
                if (_tag == null)
                {
                    TP_Tag newTag = new TP_Tag
                    {
                        TagName = item.TagName
                    };
                    db.TP_Tag.Add(newTag);
                    db.TP_ArticleTag.Add(new TP_ArticleTag
                    {
                        ArticleID = article.ID,
                        TagID = newTag.ID
                    });
                }
                else
                {
                    db.TP_ArticleTag.Add(new TP_ArticleTag
                    {
                        ArticleID = article.ID,
                        TagID = _tag.ID
                    });
                }
            }
            foreach (var img in imgs)
            {
                db.TP_ArticleImg.Add(img);
            }

            SaveChanges();
        }

        public PagedList<ArticleList> GetArticleList(string title, string aKey, string status, string aClass, DateTime? startDate, DateTime? endDate, int pageIndex, int pageSize)
        {
            var data = (from a in db.TP_Article
                        join ac in db.TP_ArticleClass on a.ID equals ac.ArticleID
                        orderby a.CreateTime descending
                        where ac.ClassID == aClass
                        select a);
            if (!string.IsNullOrEmpty(title))
                data = data.Where(x => x.Title == title);
            if (!string.IsNullOrEmpty(aKey))
                data = data.Where(x => x.AKey == aKey);
            if (!string.IsNullOrEmpty(status))
                data = data.Where(x => x.Status == status);
            if (startDate != null)
                data = data.Where(x => x.CreateTime >= startDate);
            if (endDate != null)
            {
                endDate = endDate.Value.AddDays(1);
                data = data.Where(x => x.CreateTime <= endDate);
            }
            return data.Select(x => new ArticleList
            {
                ID = x.ID,
                Title = x.Title,
                AKey = x.AKey,
                CreateTime = x.CreateTime,
                AddDateTime = x.AddDateTime,
                TitleImage = x.TitleImage,
                Status = x.Status
            }).ToPagedList(pageIndex, pageSize);
        }

        public void UpdateArticle(TP_Article article, TP_ArticleClass aClass, List<TP_ArticleBody> bodyList, List<TP_Tag> tagList, List<TP_ArticleImg> imgs)
        {
            var a = db.TP_Article.Where(x => x.ID == article.ID).FirstOrDefault();
            if (a != null)
            {
                a.TitleImage = article.TitleImage;
                a.AKey = article.AKey;
                a.AddDateTime = article.AddDateTime;
                a.Allowreply = article.Allowreply;
                a.Author = article.Author;
                a.Source = article.Source;
                a.SourceUrl = article.SourceUrl;
                a.Status = article.Status;
                a.Summary = article.Summary;
                a.Title = article.Title;
                a.UpdateBy = article.UpdateBy;
                a.UpdateTime = article.UpdateTime;
                a.FID = article.FID;
                a.FIntegral = article.FIntegral;
            }
            var oldc = db.TP_ArticleClass.Where(x => x.ArticleID == a.ID).FirstOrDefault();
            if (oldc != null)
                oldc.ClassID = aClass.ClassID;
            var oldBody = db.TP_ArticleBody.Where(x => x.ArticleID == article.ID).ToList();
            foreach (var item in oldBody)
            {
                db.TP_ArticleBody.Remove(item);
            }
            foreach (var item in bodyList)
            {
                db.TP_ArticleBody.Add(item);
            }
            var oldImg = db.TP_ArticleImg.Where(x => x.ArticleID == article.ID).ToList();
            foreach (var item in oldImg)
            {
                db.TP_ArticleImg.Remove(item);
            }
            foreach (var item in imgs)
            {
                db.TP_ArticleImg.Add(item);
            }
            var oldTag = db.TP_ArticleTag.Where(x => x.ArticleID == article.ID).ToList();
            foreach (var item in oldTag)
            {
                db.TP_ArticleTag.Remove(item);
            }
            foreach (var item in tagList)
            {
                var _tag = db.TP_Tag.Where(x => x.TagName == item.TagName).FirstOrDefault();
                if (_tag == null)
                {
                    TP_Tag newTag = new TP_Tag
                    {
                        TagName = item.TagName
                    };
                    db.TP_Tag.Add(newTag);
                    db.TP_ArticleTag.Add(new TP_ArticleTag
                    {
                        ArticleID = article.ID,
                        TagID = newTag.ID
                    });
                }
                else
                {
                    db.TP_ArticleTag.Add(new TP_ArticleTag
                    {
                        ArticleID = article.ID,
                        TagID = _tag.ID
                    });
                }
            }
            SaveChanges();
        }

        public void DeleteArticle(string id)
        {
            db.Entry<TP_Article>(new TP_Article { ID = id }).State = System.Data.Entity.EntityState.Deleted;
            var oldBody = db.TP_ArticleBody.Where(x => x.ArticleID == id).ToList();
            foreach (var item in oldBody)
            {
                db.TP_ArticleBody.Remove(item);
            }
            var oldTag = db.TP_ArticleTag.Where(x => x.ArticleID == id).ToList();
            foreach (var item in oldTag)
            {
                db.TP_ArticleTag.Remove(item);
            }
            SaveChanges();
        }

        public List<TP_AppClass> GetArticleClass()
        {
            return db.TP_AppClass.Where(x => x.ClassType == "10").OrderBy(x => x.Sort).ToList();
        }

        public void UpdateArticleImg(TP_ArticleImg img)
        {
            db.Entry<TP_ArticleImg>(img).State = System.Data.Entity.EntityState.Modified;
            SaveChanges();
        }

        public TP_ArticleImg GetArticleImg(string articleID, string id)
        {
            return db.TP_ArticleImg.Where(x => x.FileID == id && x.ArticleID == articleID).FirstOrDefault();
        }

        public FullArticleInfo GetFullArtileByID(string id)
        {
            var data = (from a in db.TP_Article
                        join abody in db.TP_ArticleBody on a.ID equals abody.ArticleID into abodys
                        join atag in db.TP_ArticleTag on a.ID equals atag.ArticleID
                        join tag in db.TP_Tag on atag.TagID equals tag.ID into tags
                        join aimg in db.TP_ArticleImg on a.ID equals aimg.ArticleID into aimgs
                        join aclass in db.TP_ArticleClass on a.ID equals aclass.ArticleID into aclasses
                        where a.ID == id
                        select new FullArticleInfo
                        {
                            TP_Article = a,
                            TP_ArticleBody = abodys,
                            TP_ArticleClass = aclasses,
                            TP_ArticleImg = aimgs,
                            TP_Tag = tags
                        }).FirstOrDefault();
            return data;
        }
    }
}
